package com.demo.common.model;

import java.math.BigDecimal;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.swing.plaf.metal.MetalIconFactory.FolderIcon16;

import org.apache.poi.hssf.record.chart.FontBasisRecord;

import com.demo.common.ResultPageList;
import com.demo.common.kit.TimeKit;
import com.demo.common.model.base.BaseMealallowance;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;

/**
 * Generated by JFinal.
 */
@SuppressWarnings("serial")
public class Mealallowance extends BaseMealallowance<Mealallowance> {
	public static final Mealallowance dao = new Mealallowance();
	
	/**
	 * 
	 * @param rows
	 * @param page
	 * @return
	 */
	public ResultPageList findPageList(int rows, int page) {
		Page<Mealallowance> resultPage = paginate(page, rows, "select *", "from mealallowance ORDER BY workTime DESC");
		ResultPageList resultPageList = new ResultPageList();
		resultPageList.setRows(resultPage.getList());
		resultPageList.setTotal(resultPage.getTotalRow());
		return resultPageList;
	}
	
	public Mealallowance getTodayWork(Date worktime) {
		return findFirst("select * from mealallowance WHERE workTime BETWEEN ? AND ?", TimeKit.getStartOverTime(worktime), TimeKit.getEndOverTime(worktime));
	}
	
	public List<Mealallowance> getByTime (String startTime, String endTime) {
		return find("SELECT * from mealallowance WHERE workTime BETWEEN ? AND ?", startTime, endTime);
	}
	
	public List<Record> getExpense(String startTime, String endTime) {
		List<Record> resultList = new ArrayList<Record>();
		List<Record> tmpList = Db.find("select * from v_expense where workTime BETWEEN ? AND ? ORDER BY workTime", startTime, endTime);
		for (Record record : tmpList) {
			try {
				int times = record.getInt("times");
				if(times <= 1) {
					resultList.add(record);
				} else {
					BigDecimal avgMoney = record.getBigDecimal("money").divide(BigDecimal.valueOf(times));
					for(int i = 0; i < times; i++) {
						Record tmp = new Record();
						tmp = record;
						tmp.set("money", avgMoney);
						resultList.add(tmp);
					}
				}
			} catch (NullPointerException e) {
				resultList.add(record);
			}
			
		}
		return resultList;
	}
	
	public List<Record> getTimeDetail(String startTime, String endTime) {
		return Db.find("SELECT CONCAT(DATE_FORMAT(startTime,'%Y/%m/%d %h:%i'), '-', DATE_FORMAT(workTime,'%h:%i')) timeDetail , jobContent FROM mealallowance  WHERE duration > 0 and workTime BETWEEN ? AND ?", startTime, endTime);
	}
	
	public List<Record> getTimeSheetValue(String startTime, String endTime) {
		Calendar startCalendar = TimeKit.stringToCalendar(startTime);
		Calendar endCalendar = TimeKit.stringToCalendar(endTime);
		List<Mealallowance> mealallowanceList = find("select * from mealallowance WHERE workTime BETWEEN ? AND ?", startTime, endTime);
		Record record = new Record();
		for(Calendar tmp = startCalendar; tmp.get(Calendar.DAY_OF_MONTH) <= endCalendar.get(Calendar.DAY_OF_MONTH); tmp.add(Calendar.DAY_OF_MONTH, 1)) {
			t1:for (Mealallowance mealallowance : mealallowanceList) {
				Calendar calendar = Calendar.getInstance();
				calendar.setTime(mealallowance.getWorkTime());
				if(calendar.get(Calendar.DAY_OF_MONTH) == tmp.get(Calendar.DAY_OF_MONTH)) {
					if(TimeKit.isWeekend(calendar)) {//如果是周末
						record.set(tmp.get(Calendar.DAY_OF_MONTH) + "", mealallowance.getDuration().setScale(1));
					} else {//如果不是周末
						record.set(tmp.get(Calendar.DAY_OF_MONTH) + "", mealallowance.getDuration().add(new BigDecimal(7.5)).setScale(1));
					}
					mealallowanceList.remove(calendar);
					break t1;
				}
			}
			if(record.get(tmp.get(Calendar.DAY_OF_MONTH) + "") == null) {
				if(!TimeKit.isWeekend(tmp)) {
					record.set(tmp.get(Calendar.DAY_OF_MONTH) + "", 7.5);
				}
			}
		}
		
		/*for (Mealallowance mealallowance : mealallowanceList) {
			int dayOfMonth = TimeKit.getDayOfMonth(mealallowance.getWorkTime());
			for (int i = startNum; i < dayOfMonth; i++) {
				if(!TimeKit.isWeekend(mealallowance.getWorkTime())){
					record.set(startNum + "", 7.5);
				}
				startNum++;
			}
			//判断是否是周末
			if(!TimeKit.isWeekend(mealallowance.getWorkTime())){
				record.set(startNum + "", mealallowance.getDuration().add(new BigDecimal(7.5)).setScale(1));
			} else {
				record.set(startNum + "", mealallowance.getDuration().setScale(1));
			}
		}*/
		List<Record> tmpList = new ArrayList<Record>();
		tmpList.add(record);
		return tmpList;
	}
}
